package com.spring.sunflower.common;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.spring.sunflower.R;
import com.spring.sunflower.anchor.PersonalDetailActivity;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.BlackListBean;
import com.spring.sunflower.constant.Constant;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/7/15<br/>
 * <p>
 * Describe:<br/>
 */
public class BlackListActivity extends BaseActivity<BlackListPresenter> implements CommonContract.BlackListview, OnLoadMoreListener {
    private RecyclerView mRecyclerView;
    private BlackListAdapter mAdapter;
    private List<BlackListBean.DatasBean> mData = new ArrayList<>();
    private String userID;
    private int pageIndex = 1;
    private SmartRefreshLayout refreshLayout;
    private int removePos = -1;
    private TextView tvEmpty;

    @Override
    public void initView() {
        tvTitle.setText("黑名单");
        mRecyclerView = findViewById(R.id.rvRecyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);
        tvEmpty = findViewById(R.id.tvEmpty);
        mAdapter = new BlackListAdapter(R.layout.adapter_item_black_list, mData);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        userID = getField(Constant.KEY_USERID, "");
        mPresenter.getBlackList(userID, String.valueOf(pageIndex));
        refreshLayout.setOnLoadMoreListener(this);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                BlackListBean.DatasBean datasBean = mData.get(position);
                Intent intent = new Intent(BlackListActivity.this, PersonalDetailActivity.class);
                intent.putExtra(Constant.KEY_TARGET_ID, String.valueOf(datasBean.getBlackMemberId()));
                startActivity(intent);
            }
        });
        mAdapter.addChildClickViewIds(R.id.btnChat);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                BlackListBean.DatasBean datasBean = mData.get(position);
                mPresenter.blackRemove(datasBean.getBlackMemberId());
                removePos = position;
            }
        });
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_black_list;
    }

    @Override
    public BlackListPresenter setPresenter() {
        return new BlackListPresenter(this);
    }

    @Override
    public void showBlackList(List<BlackListBean.DatasBean> data) {
        mAdapter.addData(data);
        showOrHideEmpty();
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadMore();
        }
    }

    public void showOrHideEmpty() {
        if (mData.size() > 0) {
            refreshLayout.setVisibility(View.VISIBLE);
            tvEmpty.setVisibility(View.GONE);
        } else {
            refreshLayout.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void blackRemoveSuccess() {
        if (removePos >= 0) {
            mAdapter.removeAt(removePos);
            removePos = -1;
            showToast("解除成功");
        }
        showOrHideEmpty();
    }

    @Override
    public void blackRemoveFailed() {
        removePos = -1;
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        mPresenter.getBlackList(userID, String.valueOf(++pageIndex));
    }
}
